//
//  MemberOrderController.m
//  OrderMeal
//
//  Created by 周浩 on 14-3-20.
//  Copyright (c) 2014年 周浩. All rights reserved.
//

#import "MemberOrderController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIView+Toast.h"
#import "MBProgressHUD.h"
#import "CustomIOS7AlertView.h"
#import "orderingGoodTableViewCell.h"
#import "shoppingCartCell.h"
@interface MemberOrderController (){
    NSString *_buildingID;
    NSMutableArray *_timePickerArray;
    MBProgressHUD *hud;
}
@property (strong, nonatomic) IBOutlet UIPickerView *timePicker;

@end

@implementation MemberOrderController
@synthesize goodsids = _goodsids;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"goodsid--->%@",self.goodsids);
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"加载数据中";
    [self requestWithTime];
    
    UIView * viewClear= [UIView new];
    [viewClear setBackgroundColor:[UIColor clearColor]];
    [self.tableView setTableFooterView:viewClear];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.username.text = [userDefaults objectForKey:@"username"];
    self.buildingName.text = [userDefaults objectForKey:@"buildingName"];
    self.address.text = [userDefaults objectForKey:@"address"];
    
    int numOfGoods = [self.goodsids count];//商品数量
    NSString *totalPriceStr = [NSString stringWithFormat:@"总共有%d个商品    总价%@",numOfGoods,self.totalPrice];
    NSLog(@"totalPriceStr--->%@",totalPriceStr);
    [self.goodsTotalInfo setText:totalPriceStr];
    
    //添加userInfoView点击收拾
    UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(modifyUserInfo)];
    [self.userInfoView addGestureRecognizer:singleTap];
}


-(void)modifyUserInfo{
    CustomIOS7AlertView *modifyUserInfoAlert = [[CustomIOS7AlertView alloc]initWithFrame:CGRectMake(0, 0, 280, 120)];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 280, 120)];
    UILabel *buildingText = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 80, 21)];
    [buildingText setText:@"送餐大厦 :"];
    UITextField *buildingField = [[UITextField alloc]initWithFrame:CGRectMake(105, 16, 160, 30)];
    [buildingField setBorderStyle:UITextBorderStyleRoundedRect];
    //送餐具体楼层及房间号
    
    UITextView *addressText = [[UITextView alloc]initWithFrame:CGRectMake(20, 50, 80, 50)];
    [addressText setText:@"送餐具体楼层及房间号 :"];
    [addressText setEditable:NO];
    [addressText setBackgroundColor:[[UIColor alloc] initWithRed:230.0f/255.0 green:230.0f/255.0 blue:230.0f/255.0 alpha:1.0]];
    addressText.font = [UIFont fontWithName:@"Helvetica Neue" size:17.0f];
    UITextField *addressTextField = [[UITextField alloc]initWithFrame:CGRectMake(105, 56, 160, 30)];
    [addressTextField setBorderStyle:UITextBorderStyleRoundedRect];
    
    
    [view addSubview:buildingText];
    [view addSubview:buildingField];
    buildingField.delegate = self;
    [view addSubview:addressText];
    [view addSubview:addressTextField];
    addressTextField.delegate = self;
    [modifyUserInfoAlert setContainerView:view];
    [modifyUserInfoAlert setButtonTitles:@[@"确认",@"取消"]];
    [modifyUserInfoAlert setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
        if (buttonIndex == 0) {
            NSLog(@"修改信息完毕");
        }
        
        
        [alertView removeFromSuperview];
    }];
    [modifyUserInfoAlert show];
}
-(void)submitWithEmail:(NSString *)email
              goodsids:(NSString *)goodsid
            buildingID:(NSString *)buildingID
               address:(NSString *)address
                  time:(NSString *)time
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    
    NSDictionary *parameters=@{
                                @"email" : email,
                                @"goodsids" : goodsid,
                                @"buildingID" :buildingID,
                                @"address" :address,
                                @"time" : time,
                                @"IPAddress":@""
                              };
    NSString *urlStr = [NSString stringWithFormat:@"%@?email=%@&goodsids=%@&buildingID=%@&address=%@&time=%@&IPAddress",memSubmit_url,email,goodsid,buildingID,address,time];
    NSLog(@"urlStr---->%@",urlStr);
    [manager GET:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber *status=responseObject[@"success"];
        if ([status isEqualToNumber:@1]) {
            [self.view makeToast:responseObject[@"msg"] duration:2.0 position:@"center"];
        }else{
            [self.view makeToast:responseObject[@"msg"] duration:2.0 position:@"center"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view makeToast:@"暂时无法提交" duration:2.0 position:@"center"];
    }];
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (IBAction)submit:(UIButton *)sender {
    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
    NSString *email=[defaults objectForKey:@"username"];
    NSString *buildingID = [defaults objectForKey:@"buildingID"];
    NSInteger timeIndex = [self.timePicker selectedRowInComponent:0];
    for (NSString *goodsid in _goodsids) {
            [self submitWithEmail:email
                        goodsids:goodsid
                        buildingID:buildingID
                        address:self.address.text
                        time:[NSString stringWithFormat:@"%d",timeIndex]];
   }
}


//pickerView实现
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [_timePickerArray count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return _timePickerArray[row];
}

//送餐时间列表
-(void)requestWithTime
{
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    [manager GET:ordertime_url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *dataArray=responseObject[@"retData"];
        _timePickerArray = [[NSMutableArray alloc]init];
        for (NSDictionary *dict in dataArray) {
            [_timePickerArray addObject:dict[@"name"]];
        }
        [self.timePicker reloadComponent:0];
        [hud removeFromSuperview];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.view makeToast:@"网络出错" duration:2.0 position:@"center"];
    }];
}

- (IBAction)pickTime:(id)sender {
    CustomIOS7AlertView *alert = [[CustomIOS7AlertView alloc]init];
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 212)];
    self.timePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(25, 25, 150, 162)];
    self.timePicker.delegate =self;
    self.timePicker.dataSource = self;
    [demoView addSubview:self.timePicker];
    [alert setContainerView:demoView];
    [alert setButtonTitles:@[@"确认"]];
    [alert show];
    [alert setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
        NSInteger row = [self.timePicker selectedRowInComponent:0];
        [self.timeBtn setTitle:_timePickerArray[row] forState:UIControlStateNormal];
    }];

}

//tableView加载数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.selectedGoods count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    orderingGoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"orderingGood"];
    NSInteger row = [indexPath row];
    NSLog(@"count --->%d",[self.selectedGoods count]);
    cell.goodInfo.text = self.selectedGoods[row][@"goodsName"];
    cell.goodPrice.text = self.selectedGoods[row][@"goodsPrice"];
    cell.goodQuantity.text = [NSString stringWithFormat:@"%@份",self.selectedGoods[row][@"goodsQuantity"]];
    return cell;
}

//修改用户信息

@end
